<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>评论列表</title>
  <link rel="stylesheet" href="./libs/bootstrap/css/bootstrap.min.css">
  <link rel="stylesheet" href="./css/reset.css">
  <link rel="stylesheet" href="./css/iconfont.css">
  <link rel="stylesheet" href="./css/main.css">
  <link rel="stylesheet" href="./libs/layui/css/layui.css">
  <script src="./libs/jquery/jquery.min.js"></script>
  <script src="./libs/layui/layui.js"></script>
</head>

<body>
  <div class="container-fluid">
    <div class="common_title">
      评论列表
    </div>
    <div class="container-fluid common_con">
      <table class="table table-striped table-bordered table-hover mp20">
        <thead>
          <tr>
            <th>作者</th>
            <th>评论</th>
            <th>评论在</th>
            <th>提交于</th>
            <th>状态</th>
            <th class="text-center" width="100">操作</th>
          </tr>
        </thead>
        <tbody id="tbody">
          <!-- <tr>
            <td>小周</td>
            <td>这是一条测试评论，欢迎光临</td>
            <td>《世界，你好》</td>
            <td>2017-07-04 12:00:00</td>
            <td>已批准</td>
            <td class="text-center">
              <a href="javascript:void(0);;" class="btn btn-warning btn-xs">拒绝</a>
              <a href="javascript:void(0);;" class="btn btn-danger btn-xs">删除</a>
            </td>
          </tr> -->

        </tbody>
      </table>
      <div class="row text-center">
        <!-- <ul class="pagination pagination-sm">
          <li class="page-item first disabled"><a href="#" class="page-link">首页</a></li>
          <li class="page-item prev disabled"><a href="#" class="page-link">上一页</a></li>
          <li class="page-item active"><a href="#" class="page-link">1</a></li>
          <li class="page-item"><a href="#" class="page-link">2</a></li>
          <li class="page-item"><a href="#" class="page-link">3</a></li>
          <li class="page-item"><a href="#" class="page-link">4</a></li>
          <li class="page-item"><a href="#" class="page-link">5</a></li>
          <li class="page-item"><a href="#" class="page-link">6</a></li>
          <li class="page-item"><a href="#" class="page-link">7</a></li>
          <li class="page-item next"><a href="#" class="page-link">下一页</a></li>
          <li class="page-item last"><a href="#" class="page-link">尾页</a></li>
        </ul> -->
        <ul id="pagination" class="pagination-sm"></ul>
      </div>

    </div>
  </div>

  <script src="./js/http.js"></script>
  <script src="./libs/art-template/template-web.js"></script>
  <script id="comment_list" type="text/html">
    {{each data}}
    {{if $value.state == '已拒绝'}}
      <tr class="danger">
    {{else}}
    <tr>
    {{/if}} 
      <td>{{$value.author}}</td>
      <td>{{$value.content}}</td>
      <td>{{$value.title}}</td>
      <td>{{$value.date}}</td>
      <td>{{$value.state}}</td>
      <td class="text-center">
        {{if $value.state == '已拒绝'}}
          <a href="javascript:void(0);;" class="btn btn-info btn-xs" onclick="pass({{$value.id}})">批准</a>
          {{else if $value.state == '已通过'}}
            <a href="javascript:void(0);;" class="btn btn-warning btn-xs" onclick="reject({{$value.id}})">拒绝</a>
          {{else}}
            <a href="javascript:void(0);;" class="btn btn-info btn-xs" onclick="pass({{$value.id}})">批准</a>
            <a href="javascript:void(0);;" class="btn btn-warning btn-xs" onclick="reject({{$value.id}})">拒绝</a>
        {{/if}}
        <a href="javascript:void(0);;" class="btn btn-danger btn-xs" onclick="delComment({{$value.id}})">删除</a>
      </td>
    </tr>
    {{/each}}
  </script>
  <script>
    $(function (page, curr) {
      get_comment_list();
    })
    function get_comment_list(curr, limit) {
      let page_params = {
        page: curr,
        perpage: limit
      }
      myAjax('GET', '/admin/comment/search', page_params, function (resData) {
        // console.log(resData);
        if (resData.code !== 200) return;
        let commentHtml = template('comment_list', resData.data);
        $('#tbody').html(commentHtml);
        // es6模板字符串进行简单判断写法
        // let comment_str = '';
        // resData.data.data.forEach(item => {
        //   let str = `
        //   <tr class="${(item.state == '已拒绝') ? 'danger' : ''}">
        //     <td>${item.author}</td>
        //     <td>${item.content}</td>
        //     <td>${item.title}</td>
        //     <td>${item.date}</td>
        //     <td>${item.state}</td>
        //     <td class="text-center">
        //       ${(item.state == '已拒绝') || (item.state == '待审核') ? '<a href="javascript:void(0);;" class="btn btn-info btn-xs" onclick="pass(${item.id})">批准</a>' : ''}
        //       ${(item.state == '已通过') || (item.state == '待审核') ? '<a href="javascript:void(0);;" class="btn btn-warning btn-xs" onclick="reject(${item.id})">拒绝</a>' : ''}
        //       <a href="javascript:void(0);;" class="btn btn-danger btn-xs" onclick="delComment(${item.id})">删除</a>
        //     </td>
        //   </tr>
        //   `
        //   comment_str += str;
        // })
        // console.log(comment_str);
        // $('#tbody').html(comment_str);
        // 分页管理
        page_render(resData.data.totalCount, curr);
      })
    }
    // 分页
    function page_render(totalCount, curr) {
      layui.use('laypage', function () {
        var laypage = layui.laypage;

        //执行一个laypage实例
        laypage.render({
          elem: 'pagination', //注意，这里的 test1 是 ID，不用加 # 号
          count: totalCount, //数据总数，从服务端得到
          curr: curr,
          limit: 6,
          jump: function (obj, first) {
            //obj包含了当前分页的所有参数，比如：
            // console.log(obj.curr); //得到当前页，以便向服务端请求对应页的数据。
            // console.log(obj.limit); //得到每页显示的条数

            //首次不执行
            if (!first) {
              //do something
              get_comment_list(obj.curr, obj.limit);
            }
          }
        });
      });
    }
    // 审核通过
    function pass(c_id) {
      myAjax('POST', '/admin/comment/pass', { id: c_id }, function (resData) {
        layui.use('layer', function () {
          let layer = layui.layer;
          layer.msg(resData.msg, { icon: 6 });
        });
        if (resData.code !== 200) return;
        get_comment_list();
      })
    }
    // 拒绝
    function reject(c_id) {
      myAjax('POST', '/admin/comment/reject', { id: c_id }, function (resData) {
        if (resData.code !== 200) return;
        get_comment_list();
      })
    }
    // 删除
    function delComment(c_id) {
      myAjax('POST', '/admin/comment/delete', { id: c_id }, function (resData) {
        if (resData.code !== 200) return;
        get_comment_list();
      })
    }
  </script>
</body>

</html>